/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CIGenetic;

import GeneticAlgorithm.Cromosome;
import GeneticAlgorithm.GeneticAdapter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @author andre
 */
public class ICAdapter implements GeneticAdapter{
	
    int topeSeleccion;
	int nroIteraciones;
    @Override
    public List<Cromosome> getInitialPopulation() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int getNumeroIteraciones() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Cromosome> algoritmoSeleccion(List<Cromosome> InitialP) {
		double []prob = new double[ InitialP.size() ];
		List< Cromosome > ans  = new ArrayList< Cromosome >();
		double sum = 0 ; 
		Random generator = new Random();
		for( Cromosome C : InitialP ){ sum += C.getFitness(); }
		for( int i = 0 ; i < InitialP.size() ; i++ ){
			Cromosome C = InitialP.get( i );
			prob[ i ] = C.getFitness() / sum;
		}
		for( int i = 0 ; i < Math.min( InitialP.size() , topeSeleccion ) ; i++ ){
			int j = generator.nextInt( InitialP.size() );
			double p = generator.nextDouble();
			while( p > 0  ){
				p -= prob[ j ];
				if( p < 0 )	break;
				j++;
				j %= InitialP.size();
			}
			ans.add( InitialP.get(i));
		}
		return ans;
    }
    
}
